package com.dimong.www.loan.ui.frag.main;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.chanven.lib.cptr.loadmore.SwipeRefreshHelper;
import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.dimong.loan.sdk.net.NetHttpCallback;
import com.dimong.loan.sdk.ui.adapter.BaseRecyclerAdapter;
import com.dimong.www.loan.R;
import com.dimong.www.loan.bean.CardBean;
import com.dimong.www.loan.bean.MainBean;
import com.dimong.www.loan.net.LoadHttpUtil;
import com.dimong.www.loan.net.response.prod.GetCardList;
import com.dimong.www.loan.ui.UIHelper;
import com.dimong.www.loan.ui.adapter.main.MainAdapter;
import com.dimong.www.loan.ui.base.BaseRefreshFragment;
import com.dimong.www.loan.utils.ToolUtil;
import com.dimong.www.loan.widget.LoanRefreshView;


public class MainTwoFrag extends BaseRefreshFragment {
    private SwipeRefreshLayout srl_frag_main_two;
    private RecyclerView rv_frag_main_two;
    private MainAdapter mainAdapter;
    private SwipeRefreshHelper mSwipeRefreshHelper;

    private int page;

    @Override
    public void beforeInitView() {

    }

    @Override
    public void initView() {
        super.initView();
        srl_frag_main_two = (SwipeRefreshLayout) findViewById(R.id.srl_frag_main_two);
        rv_frag_main_two = (RecyclerView) findViewById(R.id.rv_frag_main_two);
        rv_frag_main_two.setItemAnimator(null);
    }

    @Override
    public void setViewStatus() {
        rv_frag_main_two.setLayoutManager(new LinearLayoutManager(getActivity()));
        mainAdapter = new MainAdapter();
        rv_frag_main_two.setAdapter(new RecyclerAdapterWithHF(mainAdapter));
        mSwipeRefreshHelper = new SwipeRefreshHelper(srl_frag_main_two, rv_frag_main_two);
        mSwipeRefreshHelper.setLoadMoreEnable(true);
        mSwipeRefreshHelper.setOnSwipeRefreshListener(new SwipeRefreshHelper.OnSwipeRefreshListener() {
            @Override
            public void onfresh() {
                page = 1;
                getData();
            }
        });

        mSwipeRefreshHelper.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
                page++;
                getData();
            }
        });

        mainAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(View view, int position) {
                MainBean mainBean = (MainBean) mainAdapter.getItem(position);
                if (mainBean.getObject() instanceof CardBean) {
                    final CardBean cardBean = (CardBean) mainBean.getObject();
                    LoadHttpUtil.clickCard(cardBean.getId(), new NetHttpCallback() {
                        @Override
                        public void onPreRequest() {
                            super.onPreRequest();
                            showRefreshLoading(true);
                        }

                        @Override
                        public void onResult(String result) {
                            super.onResult(result);
                            showRefreshSuccess();
                            cardBean.setApply_num(ToolUtil.passIntPlus(cardBean.getApply_num(), 1));
                            mainAdapter.updateChange();
                            UIHelper.openWebViewAty(getActivity(), cardBean.getCard_name(), cardBean.getHref());
                        }

                        @Override
                        public void onError(String errorCode, String error) {
                            super.onError(errorCode, error);
                            showRefreshSuccess();
                            showToast(error);
                        }
                    });
                }

            }
        });

        setLoanReloadClick(new LoanRefreshView.LoanReloadClick() {
            @Override
            public void onRefreshClick() {
                showRefreshLoading();
                page = 1;
                getData();
            }
        });

        page = 1;
        showRefreshLoading();
        getData();
    }

    @Override
    public int getViewResourceId() {
        return R.layout.frag_main_two;
    }

    public void getData() {
        LoadHttpUtil.getCardList(page, new NetHttpCallback() {
            @Override
            public void onResult(String result) {
                mSwipeRefreshHelper.refreshComplete();
                super.onResult(result);
                GetCardList getCardList = gson.fromJson(result, GetCardList.class);
                showRefreshSuccess();
                srl_frag_main_two.setVisibility(View.VISIBLE);
                setData(getCardList);
            }

            @Override
            public void onError(String errorCode, String error) {
                super.onError(errorCode, error);
                mSwipeRefreshHelper.refreshComplete();
                if (mainAdapter.getItemCount() == 0) {
                    showRefreshError();
                }
                showToast(error);
            }
        });
    }

    private void setData(GetCardList getCardList) {
        if (page == 1) {
            mainAdapter.clear();
        }
        if (getCardList.getList() != null && getCardList.getList().size() != 0) {
            mSwipeRefreshHelper.loadMoreComplete(true);
            mSwipeRefreshHelper.setLoadMoreEnable(true);
            for (int i = 0; i < getCardList.getList().size(); i++) {
                MainBean bean = new MainBean();
                bean.setType(MainAdapter.ITEM_CREDIT_CARD);
                bean.setObject(getCardList.getList().get(i));
                mainAdapter.addData(bean);
            }
        } else {
            mSwipeRefreshHelper.loadMoreComplete(false);
            mSwipeRefreshHelper.setLoadMoreEnable(false);
        }

        mainAdapter.updateChange();
    }

    @Override
    public RelativeLayout getContainView() {
        return (RelativeLayout) findViewById(R.id.rl_main_frag_two);
    }
}
